package scala.org.zjt.spark

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * DESC    
  *
  * @author
  * @create 2017-05-10 下午6:07
  **/

//case class PersonEntitty(val name:String ,val age :Int )

@SerialVersionUID(1)
class PersonEntitty extends Serializable{
  @JsonProperty("name")
  var name:String = ""
  @JsonProperty("age")
  var age :Int=0

}

object JsonHandler extends App{
  val cs = new SparkConf().setMaster("local").setAppName("JsonHandler")
  val sparkContext = new SparkContext(cs)
  var mapper = new ObjectMapper()
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  val result = sparkContext.textFile("./person.txt").map{
    context =>{
      try {
        val var2 = mapper.readValue(context,classOf[PersonEntitty])
        println(var2.age + " " +var2.name)
        Some[PersonEntitty](var2)
      }catch {
        case a => println(a)
      }
    }
  }.collect().foreach(a=>println(a.asInstanceOf[Some[PersonEntitty]].get.name+"   "+a.asInstanceOf[Some[PersonEntitty]].get.age))

  sparkContext.stop()

}
